import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 余希瑶
 * @date: 2021年06月22日 19:02
 * @version:1.0
 */
public class Program1 {

    public static void main(String[] args) throws Exception{
//              需要导入javacsv Maven坐标
//        <dependency>
//            <groupId>net.sourceforge.javacsv</groupId>
//            <artifactId>javacsv</artifactId>
//            <version>2.0</version>
//        </dependency>
        //读取csv文件
        String filepath = "cust_rate.csv";
        List<String[]> list = readCsv(filepath);
        List<CustRate> content  = convertCustRateList(list);
        //写入csv文件
        WriteCsv(content);
    }

    private static List<CustRate> convertCustRateList(List<String[]> list) {
        List<CustRate> content = new ArrayList<>();
        for (int r = 1; r < list.size(); r++) {
            CustRate custRate = new CustRate();
            for (int c = 0; c < list.get(r).length; c++) {
                String cell = list.get(r)[c];
                switch (c){
                    case 0:
                        custRate.setRateType(cell);
                        break;
                    case 1:
                        custRate.setCustName(cell);
                        break;
                    case 2:
                        custRate.setRateValue(cell);
                        break;
                    case 3:
                        custRate.setRateDate(cell);
                        break;
                    default:
                        break;
                }
            }
            content.add(custRate);
        }
        return content;
    }

    //读取csv文件
    public  static List<String[]> readCsv(String filePath) throws Exception {
        List<String[]> csvList = new ArrayList<String[]>();
        if (isCsv(filePath)) {
            CsvReader reader = new CsvReader(filePath, ',', Charset.forName("utf8"));
            while (reader.readRecord()) { //逐行读入除表头的数据
                csvList.add(reader.getValues());
            }
            reader.close();
        } else {
            System.out.println("此文件不是CSV文件！");
        }
        return csvList;
    }
    //判断是否是csv文件
    private static boolean isCsv(String fileName) {
        return fileName.matches("^.+\\.(?i)(csv)$");
    }

    /**
     * 写入CSV文件
     */
    public static void WriteCsv(List<CustRate> content){
        try {
            String csvFilePath = "cust_rate_out.csv";
            CsvWriter wr =new CsvWriter(csvFilePath,',', Charset.forName("utf8"));//编码
            String [] title ={"利率类型","客户名称","报价","日期","距当日天数"};
            wr.writeRecord(title);
            for(CustRate custRate :content){
                String[] row = {custRate.getRateType(),custRate.getCustName(),custRate.getRateValue(),
                        custRate.getRateDate(),getDays(custRate.getRateDate())};
                wr.writeRecord(row);
            }
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getDays(String dateStr){
        Calendar cal=Calendar.getInstance();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            Date date = sf.parse(dateStr);
            //打印Date
            cal.setTime(date);
            long oldTime=cal.getTimeInMillis();
            long nowTime=System.currentTimeMillis();
            long days=(nowTime-oldTime)/(1000*60*60*24);//天数
            return days+"天";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "日期计算错误";
    }

    @Test
    public void get(){
        //获得一个时间格式的字符串
        String dateStr = "2016-12-31";
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式



    }

}
